package com.btc.watcher;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.ByteArrayBuffer;
import org.json.JSONObject;

// -------------------------------------------------------------------------
/**
 *  Watches MtGox btc market
 *
 *  @author Dest
 *  @version Feb 3, 2011
 */
public class BitcoinWatcher extends Activity {
    /**
     * We store references to the various TextViews here
     */
    TextView tv;
    TextView highText;
    TextView lowText;
    TextView volText;
    TextView buyText;
    TextView sellText;
    TextView lastText;
    TextView endText;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        tv = (TextView)findViewById(R.id.displayText);
        highText = (TextView)findViewById(R.id.highText);
        lowText = (TextView)findViewById(R.id.lowText);
        volText = (TextView)findViewById(R.id.volText);
        buyText = (TextView)findViewById(R.id.buyText);
        sellText = (TextView)findViewById(R.id.sellText);
        lastText = (TextView)findViewById(R.id.lastText);
        endText = (TextView)findViewById(R.id.endText);


        updateStats();
    }

    /**
     * Here we display specific info from Mt.Gox and tell the user to
     * install the Widget
     */
    public void updateStats() {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://www.mtgox.com/code/data/ticker.php");
        try {
            HttpResponse response = client.execute(post);

            InputStream is = response.getEntity().getContent();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(20);

            //convert byte array to string
            int current = 0;
            while((current = bis.read()) != -1) {
                baf.append((byte)current);
            }
            String text = new String(baf.toByteArray());
            JSONObject jObject = new JSONObject(text);
            JSONObject jTicker = jObject.getJSONObject("ticker");

            highText.setText("High: " + jTicker.getString("high"));
            lowText.setText("Low: " + jTicker.getString("low"));
            volText.setText("Vol: " + jTicker.getString("vol"));
            buyText.setText("Buy: " + jTicker.getString("buy"));
            sellText.setText("Sell: " + jTicker.getString("sell"));
            lastText.setText("Last: " + jTicker.getString("last"));
            tv.setText("Bitcoin stats retrieved from Mt Gox:\n");
            endText.setText("\nTip: Be sure to use the Widget!\n On the home screen, long press on an empty space, then select Android Widgets, then select Bitcoin Alert!");



        } catch(Exception e) {
            e.printStackTrace();
            tv.setText(getStackTrace(e));
        }
    }

    /**
     * this allows us to turn error traces into text
     * so we can display them in a TextView
     */
    public  String getStackTrace(Exception t)
    {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        t.printStackTrace(pw);
        pw.flush();
        sw.flush();
        return sw.toString();
    }
}